package CellEngine;

public class Server {
	public static CellMap map;
	public static boolean objectActionsEnabled;
	public static Logger log;
	public static ServerPort net;
	public static final String versionLong = "v" + StaticDefinitions.version + " build " + StaticDefinitions.build;
	
	public Server(String [ ] args)
	{
		if(args.length < 3)
			printHelp();
		log = new Logger(true, true, true, "HH:mm:ss");
		log.write("CellEngine " + versionLong + " initializing.");
		int iMapWidth = 0;
		int iMapHeight = 0;
		int iMapDepth = 0;
		try
		{
	        iMapWidth = Integer.parseInt(args[0]);
	        iMapHeight = Integer.parseInt(args[1]);
	        iMapDepth = Integer.parseInt(args[2]);
	    } 
		catch (NumberFormatException e) 
		{
	        close("Invalid map size specified.", false, 1);
	    }
		if(iMapWidth > StaticDefinitions.maximumMapWidth || iMapHeight > StaticDefinitions.maximumMapHeight ||  iMapDepth > StaticDefinitions.maximumMapDepth || iMapWidth < 1 || iMapHeight < 1 || iMapDepth < 1)
			close("Specified map size is smaller than 1*1*1 or exceeds " + StaticDefinitions.maximumMapWidth + "*" + StaticDefinitions.maximumMapHeight + "*" + StaticDefinitions.maximumMapDepth, false, 1);
		log.write("Generating map with a size of " + iMapWidth + "*" + iMapHeight + "*" + iMapDepth + " ...");
		long memoryUsage = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
		map = new CellMap(iMapWidth, iMapHeight, iMapDepth);
		log.write("Map generated. " + ( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memoryUsage) + " bytes of memory used.");
		close("CellEngine exiting.", true, 0);
	}
	private void initializeObjectActions()
	{
		
	}
	private void printHelp()
	{
		System.out.println("CellEngine " + versionLong);
		System.out.println("Usage: java CellEngine MapWidth MapHeight MapDepth");
		System.out.println("MapWith and MapHeight specify the X/Y map borders, MapDepth specifies the amount of Z-levels.");
		System.exit(1);
	}
	private void close(String errorMessage, boolean quiet, int exitCode)
	{
		/* Exit codes
		 * -------------------------------
		 * 0 Normal termination
		 * 1 Invalid / missing parameters
		 * 2 Runtime error
		 */
		if(!quiet)
			log.write("A fatal error has forced CellEngine to quit early.");
		log.write(errorMessage);
		System.exit(exitCode);
	}
}